package com.mlming.springboot.controller;

import com.github.pagehelper.PageInfo;
import com.mlming.springboot.enums.ResponseEnum;
import com.mlming.springboot.service.ProductService;
import com.mlming.springboot.vo.ProductDetailVo;
import com.mlming.springboot.vo.ProductVo;
import com.mlming.springboot.vo.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ProductController {
    @Autowired
    private ProductService productService;

    /**
     * 分页获取商品列表
     */
    @GetMapping("/products")
    public ResponseVo<PageInfo> getProducts(@RequestParam(required = false) Integer categoryId,
                                                  @RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                                  @RequestParam(required = false,defaultValue = "10") Integer pageSize) throws InterruptedException {
        return productService.getProducts(categoryId, pageNum, pageSize);
    }

    /**
     * 获取商品详细信息
     */
    // 由接口文档可知: 该请求是RESTFUL风格的GET请求,所以要通过@PathVariable
    @GetMapping("/products/{productId}")
    public ResponseVo<ProductDetailVo> getProductDetail(@PathVariable Integer productId) {
        if(productId == null) {
            return ResponseVo.error(ResponseEnum.PARAMS_ERROR);
        }
        return productService.getProductDetailById(productId);
    }
}
